package com.yusvn.scm.goods.fenhuo.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.aop.Inject;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yusvn.scm.common.BaseController;
import com.yusvn.scm.common.kit.RetKit;
import com.yusvn.scm.goods.fenhuo.service.ScmFenhuoPifaQihuoService;

import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;


/**
 * 批发分货 - 期货
 * @author liugz
 *
 */
@EnableCORS
public class ScmFenhuoPifaQihuoController extends BaseController {
	
	@Inject
	ScmFenhuoPifaQihuoService srv;
	
	public void index() {
		renderJson();
	}
	
	
	//表1 导出
	public void exportHh() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		kv.set("limit", 1000000); //最大导出行数
		
		//生成文件的路径及文件名称
		String fileName = "分货商品信息.xlsx"; //"c:/temp/门店级别-"+getLoginUserId()+"-"+StrKit.getRandomUUID()+".xlsx";
		//System.out.println(" filename:"+fileName);
		
		//导出（获取导出的数据）
		Page<Record> recList = srv.exportHh(kv);// System.out.println(" recordList "+recList.size() );
		List<Map<String,Object>> mapList = new LinkedList<>();
		recList.getList().forEach(r -> mapList.add(r.getColumns()));
        
		//通过工具类创建writer
		//ExcelWriter writer = ExcelUtil.getWriter(fileName);
		BigExcelWriter writer= ExcelUtil.getBigWriter(fileName); //大量数据输出
		
		try {
			//自定义标题别名
			writer.addHeaderAlias("spbh", "商品编号");
			writer.addHeaderAlias("spmc", "商品名称");
			writer.addHeaderAlias("ysmc", "颜色名称");
			writer.addHeaderAlias("ppmc", "品牌");
			writer.addHeaderAlias("nfmc", "年份");
			writer.addHeaderAlias("jjmc", "季节");
			writer.addHeaderAlias("lbmc", "类别");
			writer.addHeaderAlias("khdhsl", "客户订货");
			writer.addHeaderAlias("khdhsysl", "订货未发");
			writer.addHeaderAlias("zckc", "总仓库存");
			writer.addHeaderAlias("ypckc", "样品仓库存");
			writer.addHeaderAlias("mdkc", "门店仓库存");
			writer.addHeaderAlias("xscksl", "批发");
			writer.addHeaderAlias("xsthsl", "批退");
			writer.addHeaderAlias("cgdhsl", "采订");
			writer.addHeaderAlias("cgrksl", "采入");
			writer.addHeaderAlias("cgthsl", "采退");
			writer.addHeaderAlias("dhkhsl", "订客数");
			writer.addHeaderAlias("dhts", "到货天数");
			writer.addHeaderAlias("xsddrqmin", "最早订单日");
			writer.addHeaderAlias("cmd", "尺码段");
			writer.addHeaderAlias("fxjg", "分销价");
			
			//writer.setColumnWidth(0,20).setColumnWidth(1,20).setColumnWidth(2,30).setColumnWidth(3,20).setColumnWidth(4,20);
			//一次性写出内容，强制输出标题
			writer.write(mapList, true);
			
			HttpServletResponse response = getResponse();
	        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
	        response.setCharacterEncoding("UTF-8");
	        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
	        response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName,"utf-8") );
	        ServletOutputStream out;
			out = response.getOutputStream();
			writer.flush(out, true);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
            if(writer!=null){
                writer.close(); //关闭writer，释放内存
            }
        }
        
		renderNull();
	}
	
	//	表4 导出
	public void exportFhjgmx() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		kv.set("limit", 1000000); //最大导出行数
		
		//生成文件的路径及文件名称
		String fileName = "分货结果明细.xlsx"; //"c:/temp/门店级别-"+getLoginUserId()+"-"+StrKit.getRandomUUID()+".xlsx";
		//System.out.println(" filename:"+fileName);
		
		//导出（获取导出的数据）
		List<Record> recList = srv.exportFhjgmx(kv);// System.out.println(" recordList "+recList.size() );
		List<Map<String,Object>> mapList = new LinkedList<>();
		recList.forEach(record -> mapList.add(record.getColumns()));
        
		//通过工具类创建writer
		//ExcelWriter writer = ExcelUtil.getWriter(fileName);
		BigExcelWriter writer= ExcelUtil.getBigWriter(fileName); //大量数据输出
		
		try {
			//自定义标题别名
			writer.addHeaderAlias("khbh", "客户编号");
			writer.addHeaderAlias("khmc", "客户名称");
			writer.addHeaderAlias("fhckbh", "发货仓编号");
			writer.addHeaderAlias("fhckmc", "发货仓名称");
			writer.addHeaderAlias("spbh", "商品编号");
			writer.addHeaderAlias("spmc", "商品名称");
			writer.addHeaderAlias("ysmc", "颜色名称");
			writer.addHeaderAlias("cmbh", "尺码编号");
			writer.addHeaderAlias("cmmc", "尺码名称");
			writer.addHeaderAlias("bcfhsl", "分货数量");
			//writer.setColumnWidth(0,20).setColumnWidth(1,20).setColumnWidth(2,30).setColumnWidth(3,20).setColumnWidth(4,20);
			//一次性写出内容，强制输出标题
			writer.write(mapList, true);
			
			HttpServletResponse response = getResponse();
	        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
	        response.setCharacterEncoding("UTF-8");
	        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
	        response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName,"utf-8") );
	        ServletOutputStream out;
			out = response.getOutputStream();
			writer.flush(out, true);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
            if(writer!=null){
                writer.close(); //关闭writer，释放内存
            }
        }
        
		renderNull();
	}
	
	// 批发期货分货 - 1货号
	public void pifaQihuoHh() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			Page<Record> recPage = srv.pifaQihuoHh(kv);
			renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("查询异常："+e.getMessage()));
		}
	}
	
	// 批发期货分货 - 2客户
	public void pifaQihuoKh() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		// 查询结果到表2
		Page<Record> recPage = srv.pifaQihuoKh(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
		
	// 批发期货分货 - 3单款分货结果-展示到界面
	public void pifaQihuoJg() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		Page<Record> recPage = srv.pifaQihuoJg(kv);		
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}	
	
	// 批发期货分货 - 4结果 按客户汇总
	public void pifaQihuoJgKhhz() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		Page<Record> recPage = srv.pifaQihuoJgKhhz(kv);		
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	// 批发期货分货 - 5结果 某客户分货明细
	public void pifaQihuoJgKhmx() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		Page<Record> recPage = srv.pifaQihuoJgKhmx(kv);		
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//  批发期货分货 - 表5获取整单备注
	public void pifaQihuoJgHqbz() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		RetKit ret = srv.pifaQihuoJgHqbz(kv);		
		renderJson(ret );
	}
	
	//  批发期货分货 - 表5保存整单备注
	public void pifaQihuoJgBcbz() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		RetKit ret = srv.pifaQihuoJgBcbz(kv);		
		renderJson(ret );
	}
	
	//查询表1商品更新行颜色
	public void pifaQihuoJgGxspys() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		List<Record> recList = srv.pifaQihuoJgGxspys(kv);		
		renderJson(RetKit.ok().setData(recList) );
	}
	
	// 批发期货分货 - 查询条件 - 撤销分货(批量)
	public void pifaQihuoCxczCxfh() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		RetKit ret = srv.pifaQihuoCxczCxfh(kv);		
		renderJson(ret );
	}
	
	// 批发期货分货 - 查询条件 - 一键分货(批量)
	public void pifaQihuoCxczYjfh() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		try {
			RetKit ret = srv.pifaQihuoCxczYjfh(kv);
			renderJson(ret);
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(RetKit.fail("分货异常："+e.getMessage()));
		}
	}	


	// 批发期货分货 - 单款分货结果-表4撤销此客户
	public void pifaQihuoJgCxckh() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		renderJson(srv.pifaQihuoJgCxckh(kv) );
	}
	
	// 批发期货分货 - 单款分货结果-表3撤销此款
	public void pifaQihuoJgCxck() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}		
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		renderJson(srv.pifaQihuoJgCxck(kv) );
	}
	
	// 批发期货分货 - 单款分货结果-表3修改数据
	public void pifaQihuoJgXgsj() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		renderJson(srv.pifaQihuoJgXgsj(kv) );
	}
	
	// 批发期货分货 - 查询条件 - 生成销售单(批量)
	public void pifaQihuoCxczScxxd() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		renderJson(srv.pifaQihuoCxczScxxd(kv) );
	}
	
}
